import os


def find_main_repo(parent_dir):
    """
    找到项目根目录
    :return:
    """
    while parent_dir:
        for dirpath, dirnames, filenames in os.walk(parent_dir):
            if ".gitignore" in filenames or "requirements.txt" in filenames or "config.yaml" in filenames:
                return dirpath

            elif "api" in dirnames or "testcase" in dirnames or "testcases" in dirnames:
                return dirpath
        parent_dir = os.path.dirname(parent_dir)
        if parent_dir == "/":
            break

    return None


att_repo = os.path.dirname(__file__)
os.environ["ATT_REPO"] = att_repo

main_repo = os.environ.get("MAIN_REPO")
if not main_repo:
    runtime_path = os.getcwd()
    main_repo = find_main_repo(runtime_path)
    os.environ["MAIN_REPO"] = main_repo


def get_main_repo():
    return os.environ["MAIN_REPO"]
